using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
	public class UserData
	{
		#region Fields
		protected static SqlConnection dbConn = new SqlConnection(Config.Global.Settings.ConnectionString);
		#endregion

		#region Get Object Method
		private static object getNullObject(object obj)
		{
			return obj == null ? DBNull.Value : obj;
		}
		private static object getNullValue(object obj)
		{
			return obj == DBNull.Value ? null : obj;
		}
		#endregion

		#region Constructors
		public UserData() {}
		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the User table.
		/// </summary>
		/// <param name="userInfo"></param>
		/// <returns>ID of Saved record</returns>
		public static int? Insert(UserInfo userInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspUserInsert]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(userInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@UserName", getNullObject(userInfo.UserName));
			dbCmd.Parameters.AddWithValue("@FullName", getNullObject(userInfo.FullName));
			dbCmd.Parameters.AddWithValue("@Email", getNullObject(userInfo.Email));
			dbCmd.Parameters.AddWithValue("@Password", getNullObject(userInfo.Password));
			dbCmd.Parameters.AddWithValue("@Address", getNullObject(userInfo.Address));
			dbCmd.Parameters.AddWithValue("@Phone", getNullObject(userInfo.Phone));
			dbCmd.Parameters.AddWithValue("@Description", getNullObject(userInfo.Description));
			dbCmd.Parameters.AddWithValue("@UserStatus", getNullObject(userInfo.UserStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value);
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Updates a record in the User table.
		/// </summary>
		/// <param name="userInfo"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool Update(UserInfo userInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspUserUpdate]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@UserID", getNullObject(userInfo.UserID));
			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(userInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@UserName", getNullObject(userInfo.UserName));
			dbCmd.Parameters.AddWithValue("@FullName", getNullObject(userInfo.FullName));
			dbCmd.Parameters.AddWithValue("@Email", getNullObject(userInfo.Email));
			dbCmd.Parameters.AddWithValue("@Password", getNullObject(userInfo.Password));
			dbCmd.Parameters.AddWithValue("@Address", getNullObject(userInfo.Address));
			dbCmd.Parameters.AddWithValue("@Phone", getNullObject(userInfo.Phone));
			dbCmd.Parameters.AddWithValue("@Description", getNullObject(userInfo.Description));
			dbCmd.Parameters.AddWithValue("@UserStatus", getNullObject(userInfo.UserStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the User table by its primary key.
		/// </summary>
		/// <param name="userID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteByUserID(int userID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspUserDeleteByUserID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@UserID", userID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the User table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspUserDeleteAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects a single record from the User table.
		/// </summary>
		/// <param name="userID"></param>
		/// <returns>UserInfo: A record from the User table by its primary key.</returns>
		public static UserInfo SelectByUserID(int userID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspUserSelectByUserID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			UserInfo userInfo =  new UserInfo();

			dbCmd.Parameters.AddWithValue("@UserID", userID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.Read())
				{
					userInfo.UserID = System.Convert.ToInt32(getNullValue(dataReader["UserID"]));
					userInfo.SiteID = System.Convert.ToInt32(getNullValue(dataReader["SiteID"]));
					userInfo.UserName = System.Convert.ToString(getNullValue(dataReader["UserName"]));
					userInfo.FullName = System.Convert.ToString(getNullValue(dataReader["FullName"]));
					userInfo.Email = System.Convert.ToString(getNullValue(dataReader["Email"]));
					userInfo.Password = System.Convert.ToString(getNullValue(dataReader["Password"]));
					userInfo.Address = System.Convert.ToString(getNullValue(dataReader["Address"]));
					userInfo.Phone = System.Convert.ToString(getNullValue(dataReader["Phone"]));
					userInfo.Description = System.Convert.ToString(getNullValue(dataReader["Description"]));
					userInfo.UserStatus = System.Convert.ToBoolean(getNullValue(dataReader["UserStatus"]));
				}

				dataReader.Close();
				return userInfo;
			}
			catch (Exception)
			{
				return new UserInfo();
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the User table.
		/// </summary>
		/// <return>DataTable</return>
		public static DataTable SelectAll()
		{
			SqlCommand dbCmd = new SqlCommand("[uspUserSelectAll]",dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbUser =  new DataTable();

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbUser.Load(dataReader);
				}

				dataReader.Close();
				return dtbUser;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the User table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <return>All records from the User table by a foreign key.</return>
		public static DataTable SelectAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspUserSelectAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbUser =  new DataTable();

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbUser.Load(dataReader);
				}

				dataReader.Close();
				return dtbUser;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		#endregion
	}
}
